<template>
<view>
	<view class="footer" @click="payquery">
		<view>下单</view>
	</view>
	<view class="list" v-for="(item,index) in list" :key="index">
		<view class="check">
			<checkbox-group @change="bindChage(item.goods_id,item.num)">
				<label><checkbox color="#FFA200"/></label>
			</checkbox-group>
		</view>
		<image class="titleimg" :src="imgUrl+item.img"></image>
			<view class="content">
				<view>{{item.name}}</view>
				<view class="title">价格:{{item.price}}</view>
				<view class="title1">数量:{{item.num}}</view>
			</view>
			<view class="rightclass">
				<image @click="del(index,item.id)" class="img" src="/static/img/delete.jpg"></image>
			</view>
		</view>
	</view>
</view>
</template>
<script>
import request from '@/util/request.js'
export default {
	data() {
		return {
			imgUrl:this.url,
			list:[],
			arr:[]
		}
	},
	onLoad:function(){
		uni.showLoading({  
		    title: '加载中'  
		});
		this.getList()
	},
	methods: {
		getList:function(){
			var that = this;
			var data = {uid:uni.getStorageSync('uid')}
			request({
				url: 'cart/list',
				method: 'get',
				data:data,
				success: (res) => {
					uni.hideLoading();
					that.list = res.data.data
				}
			})
		},
		bindChage:function(id,num){
			for(var x in this.arr){
				if(this.arr[x]['id'] == id){
					this.arr.splice(x,1);
					return;
				}
			}
			var cartArr = {id:id,num:num}
			this.arr = this.arr.concat(cartArr)
		},
		del:function(index,id){
			var that = this;
			var data = {id:id}
			request({
				url: 'cart/delete',
				method: 'delete',
				data:data,
				success: (res) => {
					if(res.data.code == 1){
						that.list.splice(index,1)
						uni.showToast({
							title:'删除成功',
							icon: "success", 
							position:'bottom',
						});
						return;
					}
				}
			})
		},
		payquery:function(){
			if(this.arr.length == 0){
				uni.showToast({
					title:'请选择商品',
					icon: "none", 
					position:'bottom',
				});
				return;
			}
			var str = ''
			var num = ''
			for(var x in this.arr){
				if(str == ''){
					str = this.arr[x]['id']
					num = this.arr[x]['num']
				}else{
					str = str + ',' + this.arr[x]['id']
					num = num + ',' + this.arr[x]['num']
				}
			}
			uni.navigateTo({
				url:'./order?str='+str+'&num='+num
			})
		}
	}
}
</script>
<style>
.list {
    flex-direction: row;
    margin-top:10rpx;
    width: 100%;
    padding-left:20rpx;
    display:flex;
    justify-content: row;
    align-items: center;
	border-bottom: 5rpx solid #e6e6e6;
}
.titleimg{
	width:170rpx;height:170rpx;
}
.content{margin-left: 25rpx;padding-top: 5rpx;width:50%;}
.title{font-size:25rpx;margin-top:15rpx;color:#b30000;}
.title1{font-size:25rpx;margin-top:15rpx;color:#333;}
.img{width:40rpx;height:40rpx;margin-top:-20rpx;}
.footer{
    display: flex;
    position: fixed;
    z-index: 999;
    left: 0;
    bottom:20rpx;
    width: 90%;
    height:80rpx;
	margin-left:5%;
}
.footer view{
	width:100%;
	display: flex;
	justify-content: center;
	align-items: center;
	background:#FF7800;
	border-radius: 40rpx;
	color:#FFFFFF;
}
</style>